package com.ikas.ai.framework.core.model;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @Description: 节点输入变量
 * @Author: qi.yilai
 * @Date: 2023/10/27 14:38
 * @Version: 1.0.0
 **/
@Data
public class NodeInputVariables {

    /**
     * 获取批量数据的函数
     */
    private Set<BatchDataFunction> functions;

    /**
     * 变量
     */
    private JSONObject params;

    /**
     * 断言集合
     */
    private List<Boolean> predicts;

    public void merge(NodeInputVariables nodeInputVariables) {
        if (CollectionUtils.isEmpty(functions)) {
            this.functions = nodeInputVariables.getFunctions();
        } else if(! CollectionUtils.isEmpty(nodeInputVariables.getFunctions())) {
            this.functions.addAll(nodeInputVariables.getFunctions());
        }

        if (CollectionUtils.isEmpty(params)) {
            this.params = nodeInputVariables.getParams();
        } else if(! CollectionUtils.isEmpty(nodeInputVariables.getParams())) {
            this.params.putAll(nodeInputVariables.getParams());
        }

        if (CollectionUtils.isEmpty(predicts)) {
            this.predicts = nodeInputVariables.getPredicts();
        } else if(! CollectionUtils.isEmpty(nodeInputVariables.getPredicts())) {
            this.predicts.addAll(nodeInputVariables.getPredicts());
        }
    }

    public static NodeInputVariables copy(NodeInputVariables nodeInputVariables) {
        NodeInputVariables newVariables = NodeInputVariables.empty();
        if (!CollectionUtils.isEmpty(nodeInputVariables.getFunctions())) {
            newVariables.getFunctions().addAll(nodeInputVariables.getFunctions());
        }
        if (!CollectionUtils.isEmpty(nodeInputVariables.getParams())) {
            newVariables.getParams().putAll(nodeInputVariables.getParams());
        }
        if (!CollectionUtils.isEmpty(nodeInputVariables.getPredicts())) {
            newVariables.getPredicts().addAll(nodeInputVariables.getPredicts());
        }
        return newVariables;
    }


    public static NodeInputVariables empty() {
        NodeInputVariables nodeInputVariables = new NodeInputVariables();
        nodeInputVariables.setFunctions(Sets.newHashSet());
        nodeInputVariables.setParams(new JSONObject());
        nodeInputVariables.setPredicts(Lists.newArrayList());
        return nodeInputVariables;
    }

    public void mergeParams(JSONObject jsonObject) {
        if (CollectionUtils.isEmpty(params)) {
            this.params = jsonObject;
        } else {
            this.params.putAll(jsonObject);
        }
    }

    public void mergeParams(Map<String, Object> paramsMap) {
        if (CollectionUtils.isEmpty(params)) {
            this.params = new JSONObject(paramsMap);
        } else {
            this.params.putAll(paramsMap);
        }
    }

    public void mergeFunction(BatchDataFunction batchDataFunction) {
        if (Objects.isNull(this.functions)) {
            this.functions = Sets.newHashSet();
        }
        this.functions.add(batchDataFunction);
    }

    public void mergePredict(Boolean predict) {
        if (Objects.isNull(this.predicts)) {
            this.predicts = Lists.newArrayList();
        }
        this.predicts.add(predict);
    }

    public void clearFunctions() {
        if (Objects.nonNull(functions)) {
            functions.clear();
        }
    }

    public void clearPredicts() {
        if (Objects.nonNull(predicts)) {
            predicts.clear();
        }
    }
}
